package com.yinhe.music.yhmusic.login.validcode;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.base.BaseActivity;
import com.yinhe.music.yhmusic.log.BlankUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 *
 * @author 何晓华
 * @date 2017/9/22
 */

public class WebActivity extends BaseActivity {
    @BindView(R.id.webView)
    WebView webView;
    @BindView(R.id.progressbar)
    ProgressBar progressbar;
    private String url;
    private String title;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        ButterKnife.bind(this);
        if (getIntent()!=null){
            url = getIntent().getStringExtra("url");
            title = getIntent().getStringExtra("title");
        }

        webView.loadUrl(url);

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });

        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (progressbar != null){
                    progressbar.setVisibility(newProgress < 100
                            ? View.VISIBLE
                            : View.GONE);
                }
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                setToolbar(BlankUtil.isBlank(title)
                        ? view.getTitle()
                        : title);
            }
        });
    }
}
